﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;

namespace PrototypEAssessment.MultipleChoiceFrageErstellen
{
    public partial class MultipleChoiceFrageErstellenUserControl : UserControl
    {
        Dictionary<string,bool> _savedRows;

        protected void Page_Load(object sender, EventArgs e)
        { 
        }

        override
        protected void OnLoad(EventArgs e)
        {
            _savedRows = ViewState["AntwortHinzuView"] as Dictionary<string, bool>;
            if (_savedRows != null)
            {
                foreach (string key in _savedRows.Keys)
                {
                    TableRow row = new TableRow();
                    TableCell stringCell = new TableCell();
                    TableCell checkCell = new TableCell();
                    CheckBox box = new CheckBox();

                    stringCell.Text = key;
                    bool value;
                    _savedRows.TryGetValue(key,out value);
                    box.Checked = value;
                    checkCell.Controls.Add(box);
                    row.Cells.Add(stringCell);
                    row.Cells.Add(checkCell);

                    AntwortTable.Rows.Add(row);
                }
            }
               
        }

        protected void onAntwortHinzuClick(object sender, EventArgs e)
        {
            Table t = AntwortTable; 

            TableCell textCell = new TableCell();
            textCell.Text = antwortTextbox.Text;

            TableCell checkCell = new TableCell();
            checkCell.Controls.Add(new CheckBox());

            TableRow newRow = new TableRow();
            newRow.Cells.Add(textCell);
            newRow.Cells.Add(checkCell);
            
            AntwortTable.Rows.Add(newRow);

            antwortTextbox.Text = "";


            _savedRows = ViewState["AntwortHinzuView"] as Dictionary<string, bool>;
            if(_savedRows == null)
            {
                _savedRows = new Dictionary<string,bool>();
            }
            TableRow row = AntwortTable.Rows[AntwortTable.Rows.Count - 1];
            TableCell c = row.Cells[1];
            bool check = false;
            foreach (CheckBox cb in c.Controls)
            {
                check = cb.Checked;
            }
            _savedRows.Add(row.Cells[0].Text,check);
          
            ViewState["AntwortHinzuView"] = _savedRows;

        }

        protected void onErstellenClick(object sender, EventArgs e)
        {
            SPWeb thisWeb = SPContext.Current.Web;
            SPListItemCollection _ItemCollection = thisWeb.Lists["eAssessment - Fragen"].Items;
            SPListItem _MyItem = _ItemCollection.Add();
            if (frageTextBox.Text.Length > 30)
            {
                _MyItem["Titel"] = frageTextBox.Text.Substring(0, 30);
            }
            else
            {
                _MyItem["Titel"] = frageTextBox.Text;
            }
            _MyItem["Fragentext"] = frageTextBox.Text;
            _MyItem["Thema"] = themaTextbox.Text;
            _MyItem.Update();

            _ItemCollection = thisWeb.Lists["eAssessment - MultipleChoiceAntworten"].Items;

            foreach( TableRow row in AntwortTable.Rows ){
                SPListItem anotherItem = _ItemCollection.Add();
                if (frageTextBox.Text.Length > 30)
                {
                    anotherItem["Titel"] = frageTextBox.Text.Substring(0, 30);
                }
                else
                {
                    anotherItem["Titel"] = frageTextBox.Text;
                }

                TableCell c = AntwortTable.Rows[1].Cells[1];
                bool check = false;
                foreach (CheckBox cb in c.Controls)
                {
                    check = cb.Checked;
                }

                string eh = row.Cells[0].Text;
                anotherItem["Antwort"] = row.Cells[0].Text;
                anotherItem["Richtig"] = check.ToString();
                anotherItem["FragenID"] = _MyItem.ID;
                anotherItem.Update();
            }
        }
    }
}